import datetime

from sqlalchemy import Column, Integer, String, UniqueConstraint, Index, DateTime, Float

from QualityInspectionViolation.DbInit.mysqlDb import Base, session


class Appeal(Base):
    __tablename__ = 'appeal'
    id = Column(Integer, primary_key=True)
    violationRecordId = Column(Integer)
    customerId = Column(Integer)
    customerName = Column(String(32))
    adviserId = Column(Integer)
    adviserName = Column(String(32))
    status = Column(Integer)
    createdAt = Column(DateTime, default=datetime.datetime.now)
    updatedAt = Column(DateTime, default=datetime.datetime.now)

    def insertAppeal(self, violationRecordId, customerId, customerName, adviserId, adviserName, session):
        appeal = Appeal()
        appeal.violationRecordId = violationRecordId
        appeal.customerId = customerId
        appeal.customerName = customerName
        appeal.adviserId = adviserId
        appeal.adviserName = adviserName
        appeal.status = 1
        session.add(appeal)

    def queryAppealCountByAdviser(self, customerId, appealId, session):
        count = session.query(Appeal).\
            filter(Appeal.customerId == customerId).\
            filter(Appeal.status.in_([1,3])).\
            filter(Appeal.id != appealId).\
            count()

        print("AppealCount：", count)
        return count

    def queryAppeal(self, appealId, session):
        appeal = session.query(Appeal).\
            filter(Appeal.id == appealId).first()
        return appeal

    def updateAppeal(self, appealId, result, session):
        appeal = session.query(Appeal).\
            filter(Appeal.id == appealId).update({"status": result})
        return appeal
